package com.df.my_net_game.net.base;

import com.df.my_net_game.game_rpc.EzGameMsg;
import io.netty.channel.Channel;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;


public class EZGameCtx extends HashMap<String, Object> {
    //TODO 使用fastThreadLocal
    private static ThreadLocal<EZGameCtx> gameCtx = new ThreadLocal<EZGameCtx>();
    private AtomicInteger refCount = new AtomicInteger(0);
    private Thread msgHandleThread;


    //请求
    private Integer cmdId;//命令id
    private Long reqId;//请求唯一id
    private EzGameMsg reqMsg;
    private Object reqData;
    private Channel channel;

    //响应
    private EzGameMsg respMsg;
    public static EZGameCtx getContext() {
        return gameCtx.get();
    }
    public static void setGameCtx(EZGameCtx context) {
        gameCtx.set(context);
    }
    public static void removeContext() {
        EZGameCtx context = gameCtx.get();
        if(context != null) {
            context.cmdId = null;
            context.channel = null;
            context.reqData = null;
            context.clear();
        }
        gameCtx.remove();
    }

    public EzGameMsg getRespMsg() {
        return respMsg;
    }

    public void setRespMsg(EzGameMsg respMsg) {
        this.respMsg = respMsg;
    }

    public EzGameMsg getReqMsg() {
        return reqMsg;
    }

    public void setReqMsg(EzGameMsg reqMsg) {
        this.reqMsg = reqMsg;
    }

    public Integer getCmdId() {
        return cmdId;
    }
    public void setCmdId(Integer cmdId) {
        this.cmdId = cmdId;
    }
    public Long getReqId() {
        return reqId;
    }
    public void setReqId(Long path) {
        this.reqId = path;
    }
    public Object getReqData() {
        return reqData;
    }
    public void setReqData(Object reqData) {
        this.reqData = reqData;
    }

    public Channel getChannel() {
        return channel;
    }
    public void setChannel(Channel channel) {
        this.channel = channel;
    }


    public boolean channelIsActive() {
        if(channel == null){
            return false;
        }
        if(!channel.isActive()){
            return false;
        }
        if(!channel.isOpen()){
            return false;
        }
        return false;
    }

    //增加引用次数
    public void incrRefCount() {
        refCount.incrementAndGet();
    }
    //减少引用次数
    public void decrRefCount() {
        refCount.incrementAndGet();
    }

    public void recycle() {
        //引用次数归0
        //放入池子
    }


    //指定用户发
    public void sendMsg(Integer userId,String path,Object msg){
        //根据 userId找到channel
    }

    //指定用户广播
    public void sendMsg(List<Integer> userIds, String path, Object msg){
        //根据 userId找到channel
    }
    //全局广播
}
